goog.provide('dkpoints.raid');

/*
 * The raid object should look like this:
 *
 * var raid = {};
 * raid.info = { // Object of raid details.
 *                name: boss(es),
 *                date: UTC date/time,
 *                zone: zone,
 *                id: id
 *                _parseOrigin: {enum: Headcount | PlayerList | Server | Empty}
 *             }
 * raid.players = [ // Array of player objects.
 *                  {
 *                    playerName: name,
 *                    playerClass: class
 *                  }
 *                ]
 * raid.loot = [ // Array of loot objects.
 *              {
 *                item: itemID,
 *                name: itemName,
 *                tier: tierName,
 *                slot: slotName,
 *                looter: playerName,
 *                interest: ("Onspec"|"Offspec"|"Banked"|"Disenchanted"),
 *                price: dkpCost,
 *                icon: texture,
 *                quality: itemQ
 *              }
 *             ]
 */

/**
 * Generates a new Raid object.
 * @constructor
 */
dkpoints.raid.Raid = function() {
  // Use bracket-notation to preserve field names so the object can be serialized.
  // Otherwise, the server receives an object with obfuscated property names
  this["info"] = {};
  this["players"] = [];
  this["loot"] = [];
};

/**
 * Cloe this raid object.
 * @return {dkpoints.raid.Raid} The cloned object.
 */

dkpoints.raid.Raid.prototype.clone = function() {
  // The NEW raid.
  var newRaid = new dkpoints.raid.Raid();
  
  // Dirty flag used to make note of changed items.
  var dirty = false;
  
  // Clone "info" fields.
  if( this["info"] ) {
    // Set up references to the existing raid's info object (eInfo) and the new one.
    var eInfo = this["info"];
    var nInfo = newRaid["info"];
    
    if(eInfo["name"]) {  nInfo["name"] = eInfo["name"];  }
    if(eInfo["date"]) {  nInfo["date"] = eInfo["date"];  }
    if(eInfo["zone"]) {  nInfo["zone"] = eInfo["zone"];  }
    if(eInfo["id"]) {  nInfo["id"] = eInfo["id"];  }
    if(eInfo["_parseOrigin"]) {  nInfo["_parseOrigin"] = eInfo["_parseOrigin"];  }
    if(eInfo["dirty"])  {  dirty = nInfo["dirty"] = true;  }
  }
  
  if( this["players"] ) {
    // Set up references to the existing raid's player array (ePlayers) and the new one.
    var ePlayers = this["players"];
    var nPlayers = newRaid["players"];
    
    // Clone all of the player objects in the existing raid's player array.
    for(var i = 0; i < ePlayers.length; ++i) {
      var ePlayer = ePlayers[i];
      var nPlayer = {};
      
      // Only clone a player if a name exists.
      // There shouldn't be a class without a name, but who knows?
      if(ePlayer["playerName"]) {
        nPlayer["playerName"] = ePlayer["playerName"];
        if(ePlayers[i]["playerClass"]) {
          nPlayer["playerClass"] = ePlayer["playerClass"];
        }
        else {
          // Existing player object didn't have a class.  Put in the generic.
          nPlayer["playerClass"] = "noclass";
        }
        
        // If the player is marked as changed
        if(ePlayer["dirty"] || ePlayer["delete"]) {
          dirty = nPlayer["dirty"] = true;
          
          if(ePlayer["delete"]) {
            nPlayer["delete"] = true;
          }
        }
        nPlayers.push(nPlayer);
      }
    }
  }
  
  if( this["loot"] ) {
    // Set up references to the existing raid's loot array (eLoot) and the new one.
    var eLoot = this["loot"];
    var nLoot = newRaid["loot"];
    
    for(var j = 0; j < eLoot.length; ++j) {
      var eItem = eLoot[j];
      var nItem = {};
      
      if(eItem["item"]) {  nItem["item"] = eItem["item"];  }
      if(eItem["name"]) {  nItem["name"] = eItem["name"];  }
      if(eItem["tier"]) {  nItem["tier"] = eItem["tier"];  }
      if(eItem["slot"]) {  nItem["slot"] = eItem["slot"];  }
      if(eItem["looter"]) {  nItem["looter"] = eItem["looter"];  }
      if(eItem["interest"]) {  nItem["interest"] = eItem["interest"];  }
      if(eItem["price"]) {  nItem["price"] = eItem["price"];  }
      if(eItem["icon"]) {  nItem["icon"] = eItem["icon"];  }
      if(eItem["quality"]) {  nItem["quality"] = eItem["quality"];  }
      
      if(eItem["dirty"] || eItem["delete"]) {
        dirty = nItem["dirty"] = true;
        
        if(eItem["delete"]) {
          nItem["delete"] = true;
        }
      }
      nLoot.push(nItem);
    }
  }
  
  // Make sure the raid object knows it's been changed, even if the original didn't.
  if(dirty && (! this["info"]["dirty"])) {
    newRaid["info"]["dirty"] = true;
  }
  
  return newRaid;
};

/**
 * Get the class of the named player
 * @param {string} name Name of the player
 * @return {string}
 */
dkpoints.raid.Raid.prototype.getPlayerClass = function(name) {
  var members = this["players"];

  for(var i = 0; i < members.length; ++i) {
    if(members[i]["playerName"] == name) {
      return members[i]["playerClass"];
    }
  }
  
  // Player wasn't in the raid.  Return default.
  return "noclass";
};

/**
 * Sets the tier for this raid
 * @param {number} tier This raid's tier
 */
dkpoints.raid.Raid.prototype.setTier = function(tier) {
  var me = this;
  var loot = me["loot"];
  
  for(var i = 0, len = loot.length; i < len; ++i) {
    var thisLoot = loot[i];
    
    // Only set the tier if it's undefined.
    if(! goog.isDef(thisLoot["tier"])) {
      thisLoot["tier"] = parseFloat(tier);
      me["info"]["dirty"] = thisLoot["dirty"] = true;
    }
  }
  
  me.tier = tier;
};